package com.google.api.client.auth.oauth;

import com.google.api.client.http.*;

import java.io.IOException;

/**
 * Generic OAuth 1.0a URL to request a temporary or long-lived token from an authorization server.
 * @author Mischa Dasberg
 * @since 1.15
 */
public class AbstractOAuthToken extends GenericUrl {
    /**
     * HTTP transport required for executing request in {@link #execute()}.
     * @since 1.3
     */
    private HttpTransport transport;

    /** Required identifier portion of the client credentials (equivalent to a username). */
    private String consumerKey;

    /** Required OAuth signature algorithm. */
    private OAuthSigner signer;

    /** The http method to use. */
    private String httpMethod;

    /**
     * Constructor.
     * @param authorizationServerUrl encoded authorization server URL
     * @param httpMethod             The http method.
     */
    protected AbstractOAuthToken(final String authorizationServerUrl, final String httpMethod) {
        super(authorizationServerUrl);
        this.httpMethod = HttpMethods.POST.equals(httpMethod) ? HttpMethods.POST : HttpMethods.GET;
    }

    /**
     * Executes the HTTP request for a temporary or long-lived token.
     * @return OAuth credentials response object
     */
    public final OAuthCredentialsResponse execute() throws IOException {
        final HttpRequestFactory requestFactory = transport.createRequestFactory();
        final HttpRequest request = requestFactory.buildRequest(httpMethod, this, null);
        createParameters().intercept(request);
        final HttpResponse response = request.execute();
        response.setContentLoggingLimit(0);
        final OAuthCredentialsResponse oauthResponse = new OAuthCredentialsResponse();
        UrlEncodedParser.parse(response.parseAsString(), oauthResponse);
        return oauthResponse;
    }

    /**
     * Returns a new instance of the OAuth authentication provider. Subclasses may override by calling
     * this super implementation and then adding OAuth parameters.
     */
    public OAuthParameters createParameters() {
        OAuthParameters result = new OAuthParameters();
        result.consumerKey = consumerKey;
        result.signer = signer;
        return result;
    }

    /**
     * Get the HttpTransport.
     * @return transport The HttpTransport.
     */
    public HttpTransport getTransport() {
        return transport;
    }

    /**
     * Set the HttpTransport.
     * @param transport The HttpTransport.
     */
    public void setTransport(final HttpTransport transport) {
        this.transport = transport;
    }

    /**
     * Get the consumer key.
     * @return consumerKey The consumer key.
     */
    public String getConsumerKey() {
        return consumerKey;
    }


    /**
     * Set the consumer key.
     * @param consumerKey The consumer key.
     */
    public void setConsumerKey(final String consumerKey) {
        this.consumerKey = consumerKey;
    }

    /**
     * Get the oauth signer.
     * @return signer The OAuthSigner.
     */
    public OAuthSigner getSigner() {
        return signer;
    }

    /**
     * Set the oauth signer.
     * @param signer The OAuthSigner.
     */
    public void setSigner(final OAuthSigner signer) {
        this.signer = signer;
    }

    /**
     * Get the http method.
     * @return httpMethod The http method.
     */
    public String getHttpMethod() {
        return httpMethod;
    }
}
